/**
 * 
 * @authors youzhao.zhou@pandauu.com
 * @date    2016-10-26 18:00:33
 * @version $Id$
 *
 * 图片点击放大
 *     点击回退键关闭图片
 *     点击屏幕任意位置关闭图片
 *     
 *
 */

;(function(root,factory){

  if(typeof exports === 'object' && typeof module === 'object'){
    
    factory(exports);

  } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['exports'], factory);
  } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
    // CommonJS
    factory(exports);
  } else {
    // Browser globals
    if (!root._s) {
      root._s = {};
    }
    factory(root);
  }
})(typeof window !== 'undefined' ? window : this ,function(exports, undefined){
  'use strict';

  function moduleInit($, log, utils, IScroll){

    /*
      调试开关
    */
    log._logToggle(true);
    
    var myScroll = null;
    var IScroll = IScroll;
    var pubsub = utils.pubSub()||{};
    var callback = {};
    var callbackFuc = utils.callbackFuc;

    function receiver(identifier, options){
      switch(identifier){
        case 'enlarge':
          pubsub.fireEvent(identifier, enlarge(identifier, options));
        break;
      }
    };


    pubsub.addEvent('startScroll',function(){
      startScroll();
    });

    pubsub.addEvent('startBackEvent',function(){
      window.history.pushState('enlarge','图片放大后退','#/enlarge');
      window.addEventListener('popstate',backCallback);
    });

    pubsub.addEvent('showBg',function(){
      $('.jsEnlargeBg').removeClass('s-hide');
      $('.jsEnlarge').css({
        width : '27px',
        height : '27px'
      });
    });

    pubsub.addEvent('removePopState',function(){
      window.removeEventListener('popstate',backCallback);
    });

    /*
      C
    */

    function enlarge(){
      $('.service').on('click', function(event) {
        

        /*
          事件委托
            图片放大
        */

        /* Act on the event */

        var eventTarget = $(event.target).data('event');

        switch(eventTarget){
          case 'enImg':
            var img = new Image();

            var width = window.screen.width<=window.innerWidth? window.screen.width:window.innerWidth;
            var height = window.screen.height<=window.innerHeight? window.screen.height:window.innerHeight;

            img.onload = function(){
              /*
                图片的宽高自动计算
              */
              $('.jsEnlarge').css({
                width : 'auto',
                height : 'auto'
              });
              /*
                设置img标签的src属性
              */
              $('.jsEnlarge').attr({
                'data-src' : $('.jsEnlarge').attr('src'),
                'src' : $(event.target).data('img')
              });

              /*
                图片的宽高比大于屏幕的宽高时，图片为小图，居中显示
                图片的宽高比小于屏幕的宽高时，图片为长图，置顶显示
              */
              if (img.width/img.height > width/height) {
                $('#scroller').addClass('img-enlarge');
              }else{
                $('#scroller').removeClass('img-enlarge');
                pubsub.fireEvent('startScroll');
              }
            };

            img.src = $(event.target).data('img');

            pubsub.fireEvent('startBackEvent');
            pubsub.fireEvent('showBg');

            break;
          case 'close':

            window.history.back();

            break;
        }
      });
    }

    function startScroll () {
      myScroll = new IScroll('#wrapper', {
        zoom: true,
        scrollX: true,
        scrollY: true,
        mouseWheel: true,
        tap: true,
        click: true,
        wheelAction: 'zoom'
      });
    }

    function backCallback(){
      if (myScroll) {
        myScroll.destroy();
        myScroll = null;
      }
      
      $('.jsEnlargeBg').addClass('s-hide');
      $('.jsEnlarge').attr({
          'src' : $('.jsEnlarge').data('src')
      });
      $('.jsEnlarge').css({
        width : 'auto',
        height : 'auto'
      });
      $('#scroller').addClass('img-enlarge');

      pubsub.fireEvent('removePopState');
    }

    return {
      set: function(identifier, callbacks){
        callback[identifier] = [callbacks];

        pubsub.addEvent(identifier, function(data){
          console.log(callback[identifier]);
          callbackFuc(callback[identifier],data);
        });
      },
      start: function(identifier, options){
        receiver(identifier, options);
      }
    };
  }

  exports.enlarge = moduleInit;

});
